﻿using System.Net;
using System.Text;

namespace Mangdot.Webchat.Common
{
    public class RerestService
    {
        public static string Post(string url, string postString)
        {
            //Logger.Info("POST请求:" + url + "?" + postString);
            byte[] postData = Encoding.UTF8.GetBytes(postString);//编码，尤其是汉字，事先要看下抓取网页的编码方式  
            WebClient webClient = new WebClient();
            webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");//采取POST方式必须加的header，如果改为GET方式的话就去掉这句话即可  
            byte[] responseData = webClient.UploadData(url, "POST", postData);//得到返回字符流  
            string srcString = Encoding.UTF8.GetString(responseData);//解码  
            return srcString;
        }

        public static string Get(string url, string parameters = "")
        {
            Logger.Info("GET请求：" + url + "?" + parameters);
            //byte[] pData = Encoding.UTF8.GetBytes(parameters);//编码，尤其是汉字，事先要看下抓取网页的编码方式  

            if (!string.IsNullOrEmpty(parameters))
            {
                url = url + "?" + parameters;
            }

            WebClient webClient = new WebClient();
            byte[] responseData = webClient.DownloadData(url);//得到返回字符流  
            string srcString = Encoding.UTF8.GetString(responseData);//解码  
            return srcString;
        }
    }
}